package com.beiding.businessplatform.utils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class TypeUtils {

    public static Class fieldType(Field field) {
        Class<?> type = field.getType();
        if (List.class.isAssignableFrom(type)) {
            Type genericType = field.getGenericType();
            if (genericType instanceof ParameterizedType) {
                ParameterizedType t = (ParameterizedType) genericType;
                Type[] actualTypeArguments = t.getActualTypeArguments();
                if (actualTypeArguments.length == 1) {
                    return (Class) actualTypeArguments[0];
                } else {
                    return Object.class;
                }
            } else {
                return Object.class;
            }
        } else {
            return type;
        }
    }
}
